package com.ayuan.blog.controller;

import com.ayuan.blog.bean.ArticleInfo;
import com.ayuan.blog.bean.ArticleSearchBean;
import com.ayuan.blog.bean.CommonResponseBean;
import com.ayuan.blog.bean.RecentArticleSearchBean;
import com.ayuan.blog.domain.Article;
import com.ayuan.blog.domain.Classification;
import com.ayuan.blog.domain.Tag;
import com.ayuan.blog.domain.sch.ArticleSch;
import com.ayuan.blog.service.ArticleService;
import com.ayuan.blog.service.ClassificationService;
import com.ayuan.blog.service.TagService;
import com.github.pagehelper.PageInfo;
import org.durcframework.core.expression.ExpressionQuery;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by xiaozhu on 15/12/14.
 */
@Controller
public class BlogController extends BaseController{

    @Autowired
    ArticleService articleService;
    @Autowired
    ClassificationService classificationService;
    @Autowired
    TagService tagService;

    @RequestMapping(value = "list", method = RequestMethod.POST)
    @ResponseBody
    public Object list(Integer pageNum, Integer pageSize, String searchName) {
        if (null == pageNum) {
            pageNum = 1;
        }
        if (null == pageSize) {
            pageSize = 10;
        }
        ArticleSearchBean articleSearchBean = new ArticleSearchBean();
        articleSearchBean.setSearchValue(searchName);
        articleSearchBean.setPageNum(pageNum);
        articleSearchBean.setPageSize(pageSize);

        PageInfo<ArticleInfo> page = articleService.findPageArticleInfoByConditions(articleSearchBean);

        return new CommonResponseBean<PageInfo>(page);
    }

    @RequestMapping(value = "recentList", method = RequestMethod.POST)
    @ResponseBody
    public Object recentList() {
        RecentArticleSearchBean recentArticleSearchBean = new RecentArticleSearchBean();
        recentArticleSearchBean.setSize(7);

        List<ArticleInfo> articleInfoList = articleService.findRecentArticles(recentArticleSearchBean);

        return new CommonResponseBean<List>(articleInfoList);
    }

    @RequestMapping(value = "/my/list", method = RequestMethod.POST)
    @ResponseBody
    public Object myList(HttpServletRequest request, Integer pageNum, Integer pageSize, String searchName) {
        if (null == pageNum) {
            pageNum = 1;
        }
        if (null == pageSize) {
            pageSize = 10;
        }
        ArticleSearchBean articleSearchBean = new ArticleSearchBean();
        articleSearchBean.setUserId(getCurrentUser(request).getId());
        articleSearchBean.setSearchValue(searchName);
        articleSearchBean.setPageNum(pageNum);
        articleSearchBean.setPageSize(pageSize);

        PageInfo<ArticleInfo> page = articleService.findPageArticleInfoByConditions(articleSearchBean);

        return new CommonResponseBean<PageInfo>(page);
    }

    @RequestMapping(value = "my/recentList", method = RequestMethod.POST)
    @ResponseBody
    public Object myRecentList(HttpServletRequest request) {
        RecentArticleSearchBean recentArticleSearchBean = new RecentArticleSearchBean();
        recentArticleSearchBean.setSize(7);
        recentArticleSearchBean.setUserId(getCurrentUser(request).getId());
        List<ArticleInfo> articleInfoList = articleService.findRecentArticles(recentArticleSearchBean);

        return new CommonResponseBean<List>(articleInfoList);
    }

    @RequestMapping(value = "/new", method = RequestMethod.POST)
    @ResponseBody
    public Object doAdd(HttpServletRequest request, ArticleInfo articleInfo) {
        articleInfo.setUserId(getCurrentUser(request).getId());
        articleInfo.setUserName(getCurrentUser(request).getUserNickname());
        articleService.saveArticleInfo(articleInfo);

        return new CommonResponseBean<Object>(0, "新建文章成功", articleInfo);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object doEdit(HttpServletResponse response, ArticleInfo articleInfo) {
        articleService.updateArticleInfo(articleInfo);
        return new CommonResponseBean<Object>();
    }

    @RequestMapping(value = "/detail", method = RequestMethod.POST)
    @ResponseBody
    public Object getDetail(@RequestParam("id") Integer articleId) {

        ArticleInfo articleInfo = articleService.findArticleInfoByArticleId(articleId);
        articleService.view(articleId);

        return articleInfo;
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Object delete(@RequestParam("id") Integer articleId) {
        articleService.delete(articleId);

        return new CommonResponseBean<Object>();
    }
}
